import { QianfanProvider } from "./QianfanProvider";
import { OpenAIProvider } from "./OpenAIProvider";
import { configManager } from "../config";

export function createProvider(providerName: string) {
  const config = configManager.get();
  const providerConfig = config.providerConfigs[providerName] || {};

  switch (providerName) {
    case "qianfan":
      if (!providerConfig.accessKey || !providerConfig.secretKey) {
        throw new Error(
          "缺少千帆API配置：请在设置中配置 accessKey 和 secretKey"
        );
      }
      return new QianfanProvider(
        providerConfig.accessKey,
        providerConfig.secretKey
      );
    case "aliyun":
      if (!providerConfig.apiKey || !providerConfig.baseUrl) {
        throw new Error(
          "缺少阿里云百炼API配置：请在设置中配置 apiKey 和 baseUrl"
        );
      }
      return new OpenAIProvider(providerConfig.apiKey, providerConfig.baseUrl);
    default:
      throw new Error(`不支持的AI服务提供商: ${providerName}`);
  }
}
